package org.hsieh.tr.explore.common.ui;

import java.util.List;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.hsieh.tr.explore.annotation.AnnotationParser;
import org.hsieh.tr.explore.annotation.TreeNodeChildren;
import org.hsieh.tr.explore.annotation.TreeNodeParent;

@SuppressWarnings("unchecked")
public final class AnnotationContentProvider implements ITreeContentProvider {

    public Object[] getChildren(Object parentElement) {
	Object value = AnnotationParser.getValueAnnotationWith(parentElement,
		TreeNodeChildren.class);
	if (value == null)
	    return null;

	if (value instanceof List) {
	    return ((List) value).toArray(new Object[0]);
	}
	return null;
    }

    public Object getParent(Object element) {
	return AnnotationParser.getValueAnnotationWith(element,
		TreeNodeParent.class);
    }

    public boolean hasChildren(Object element) {
	TreeNodeChildren annotation = AnnotationParser.findAnnotation(element,
		TreeNodeChildren.class);
	if (annotation == null) {
	    return false;
	} else
	    return annotation.hasChildren();
    }

    public Object[] getElements(Object inputElement) {
	if (inputElement instanceof List) {
	    return ((List) inputElement).toArray(new Object[0]);
	}
	return null;
    }

    public void dispose() {

    }

    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

    }

}
